<?php
session_start();

require("../../conexao/ConectaMysql.class.php");
require("../../conexao/ControleAcesso.class.php");

require("../../cadastro/setor/setor.class.php");
require("../../cadastro/setor/gerencia_setor.class.php");

require("../../cadastro/funcionario/funcionario.class.php");
require("../../cadastro/funcionario/gerencia_funcionario.class.php");

require("../categoria/categoria.class.php");
require("../categoria/gerencia_categoria.class.php");

require("../gestao/gestao.class.php");
require("../gestao/gerencia_gestao.class.php");

require("../situacao/situacao.class.php");
require("../situacao/gerencia_situacao.class.php");

require("../cadastro/helpdesk.class.php");
require("../cadastro/gerencia_helpdesk.class.php");

require("resposta.class.php");
require("gerencia_resposta.class.php");

ControleAcesso::verificaSessao();

$ACE_PRIVid = $_SESSION['PRIVid'];
$ACE_FUNid 	= $_SESSION['FUNid'];

$conexao 				= new ConectaMysql();
$GerenciaSetor 			= new GerenciaSetor($conexao);
$GerenciaCategoria 		= new GerenciaCategoria($conexao);
$GerenciaFuncionario 	= new GerenciaFuncionario($conexao);
$GerenciaHelpdesk   	= new GerenciaHelpdesk($conexao);
$GerenciaSituacao  		= new GerenciaSituacao($conexao);

if (isset($_POST['Cadastrar'])){
	
	$HELPid 		= $_POST['HELPid'];
	$FUNid_dest 	= $_POST['FUNid_dest'];
	$SITid 			= $_POST['SITid'];
	$RESPconteudo 	= $_POST['RESPconteudo'];	
	
	$result_helpdesk = $GerenciaHelpdesk->selectHelpdesk($HELPid);
	foreach($result_helpdesk as $row_helpdesk){  
		$HELP_SETid	  		= $row_helpdesk['SETid'];
		$HELP_SETnome		= $row_helpdesk['SETnome'];
		$HELP_SETsigla		= $row_helpdesk['SETsigla'];

		//////////////setor remetente/////////////////////
		$setorRem = new Setor($HELP_SETid,$HELP_SETnome, $HELP_SETsigla);
		
		$HELP_FUNid    		= $row_helpdesk['FUNid'];
		$HELP_FUNnome    	= $row_helpdesk['FUNnome'];
		$HELP_FUNsobrenome  = $row_helpdesk['FUNsobrenome'];
		
		//////////funcionario remetente//////////////
		$funcionarioRem = new Funcionario($HELP_FUNid, $HELP_FUNnome, $HELP_FUNsobrenome, $setorRem);
		
		
		//////////////setor destinatario///////////////////
	
		$HELP_DEST_SETid	= $row_helpdesk['HELP_DEST_SETid'];
	
		$result_setor = $GerenciaSetor->selectSetor($HELP_DEST_SETid);
		foreach($result_setor as $row_setor){  
			$SETnome_dest	 = $row_setor["SETnome"];
			$SETsigla_dest    = $row_setor["SETsigla"];
		}
		
		$setorDest = new setor($HELP_DEST_SETid, $SETnome_dest, $SETsigla_dest);
		
		//////////////////funcionario Destinatario/////////

		$result_funcionarioDest = $GerenciaFuncionario->selectFuncionario($FUNid_dest, true);
		foreach($result_funcionarioDest as $row_funcionarioDest){ 
			$FUNnomeDest 		= $row_funcionarioDest['FUNnome'];
			$FUNsobrenomeDest 	= $row_funcionarioDest['FUNsobrenome'];
		}
		$funcionarioDest = new Funcionario($FUNid_dest, $FUNnomeDest, $FUNsobrenomeDest, $setorDest);
		
			
		/////////////categoria/////////////////
		$HELP_CATid = $row_helpdesk['CATid'];

		$result_categoria = $GerenciaCategoria->selectCategoria($HELP_CATid);
		foreach($result_categoria as $row_categoria){ 
			$CATdescricao 	= $row_categoria['CATdescricao'];	
		}
		
		$categoria = new Categoria($HELP_CATid, $CATdescricao);	

		
		////////////////Helpdesk//////////////////////
		
		$HELPtitulo   = $row_helpdesk['HELPtitulo'];
		$HELPconteudo = $row_helpdesk['HELPconteudo'];
		
		
		$helpdesk = new helpdesk($HELPid, $HELPtitulo, $HELPconteudo, $funcionarioRem, $categoria, $setorDest);
	
		
		
		////////////////////situacao/////////////////

		$GerenciaSituacao = new GerenciaSituacao($conexao);
		$result_situacao = $GerenciaSituacao->selectSituacao($SITid);
		foreach($result_situacao as $row_situacao){  
			$SITdescricao = $row_situacao["SITdescricao"];
		}
		
		
		$situacao = new Situacao($SITid,$SITdescricao);
		
		////////////////////Gestao///////////////////
		
		$gestao = new Gestao($helpdesk, $situacao);

		$gerenciaGestao = new GerenciaGestao($conexao);
		$gerenciaGestao->updateGestao($gestao);
	
		////////////////Resposta///////////////////
		
		$gerenciaResposta = new GerenciaResposta($conexao);
	
		$result_resposta = $gerenciaResposta->selectMaxId();
		foreach($result_resposta  as $row_resposta){  
			$RESPid = $row_resposta['RESPid'] + 1;
		}		

		$resposta = new Resposta($RESPid, $RESPconteudo, $funcionarioDest, $helpdesk);
		
		$gerenciaResposta->insertResposta($resposta);

	}
	
	
	
	
}else if (isset($_GET['HELPid'])){
	
	$HELPid = $_GET['HELPid'];
	$result_helpdesk = $GerenciaHelpdesk->selectHelpdesk($HELPid);
	foreach($result_helpdesk as $row_helpdesk){  
		$HELP_SETid	  		= $row_helpdesk['SETid'];
		$HELP_FUNid    		= $row_helpdesk['FUNid'];
		$HELPtitulo 		= $row_helpdesk['HELPtitulo'];
		$HELPconteudo 		= $row_helpdesk['HELPconteudo'];
		$HELP_CATid 		= $row_helpdesk['CATid'];
		$HELP_DEST_SETid	= $row_helpdesk['HELP_DEST_SETid'];	
	}
	
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Resposta</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../../estrutura/css/estruturaCadastro.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="../../estrutura/js/jquery.js"></script>
<script type="text/javascript" src="../../estrutura/js/jquery.validate.js"></script>
<script type="text/javascript" src="../../estrutura/js/validacaoResposta.js"></script>

</head>

<body>
<div align="left">
  <form action="inserir_resposta.php" method="post" name="formResposta" id="formResposta" target="_self">
    <input type="hidden" id="HELPid" name="HELPid" value="<?php echo "$HELPid"; ?>">
   	  <table width="90%"  border="0" align="center">
          <tr>
           	  <td colspan="2" bgcolor="#DFEDF7" class="texto"><div align="left"></div>
           	    <strong>Informe os dados da Resposta</strong></td>
          </tr>
          <tr>
              <td width="21%" bgcolor="#FFFFFF" class="texto">&nbsp;</td>
              <td width="79%">&nbsp;</td>
          </tr>
          <tr>
              <td bgcolor="#F1F4F8" class="texto">Setor</td>
              <td>
               	  <label>
                	<select id="SETid" name="SETid" DISABLED>
                    	<?php 
                		$result_setor = $GerenciaSetor->selectSetor($SETid);					
                    	foreach($result_setor as $row_setor){ 
							$SETid 		= $row_setor['SETid'];
							$SETsigla 	= $row_setor['SETsigla'];
                    		?>
                            <option value="<?php echo "$SETid";?>" <?php if($HELP_SETid == $SETid ){echo "SELECTED";}?>><?php echo "$SETsigla";?></option>
						<?php }?>
                    </select>
               	  </label>
                  </td>
          </tr>
          <tr>
            <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Funcion&aacute;rio: </span></td>
            <td>
           	  <label>
                  <select id="FUNid" name="FUNid" DISABLED>
                    <?php 
                    	$result_funcionario = $GerenciaFuncionario->selectFuncionario($HELP_FUNid, true); 
                        foreach($result_funcionario as $row_funcionario){  
                        	$FUNnome 		= $row_funcionario['FUNnome'];
                            $FUNsobrenome 	= $row_funcionario['FUNsobrenome'];
                            $FUNid			= $row_funcionario['FUNid'];
							?>
                   		  <option value="<?php echo "$FUNid";?>" <?php if($FUNid == $HELP_FUNid){echo "SELECTED";}?>><?php echo "$FUNnome $FUNsobrenome";?></option>
                      <?php }?>
                  </select>
              </label>                
            </td>
          </tr>
          <tr>
            <td bgcolor="#F1F4F8" class="texto style41">T&iacute;tulo</td>
            <td>
           	  <label>
               	  <input type="text" name="HELPtitulo" id="HELPtitulo" size="40" maxlength="35" value="<?php echo "$HELPtitulo"; ?>" DISABLED>
              </label>
            </td>
          </tr>
          <tr>
            <td valign="top" bgcolor="#F1F4F8" class="texto style41">Conte&uacute;do</td>
            <td>
           	  <label>
           	    <textarea name="HELPconteudo" id="HELPconteudo" cols="35" rows="5" DISABLED><?php echo "$HELPconteudo"; ?></textarea>
              </label>
            </td>
          </tr>
          <tr>
            <td bgcolor="#F1F4F8" class="texto style41">Categoria</td>
            <td>
           	  <label>
              	<select id="CATid" name="CATid" DISABLED>
                	<option value="">Escolha uma categoria</option>
					<?php 
                    $result_categoria = $GerenciaCategoria->selectCategoria($HELP_CATid);
                    
                    foreach($result_categoria as $row_categoria){ 
						$CATid 			= $row_categoria['CATid'];
						$CATdescricao 	= $row_categoria['CATdescricao'];
						?>
						<option value="<?php echo "$CATid"; ?>" <?php if($CATid == $HELP_CATid){echo "SELECTED";}?>><?php echo "$CATdescricao"; ?></option>
                    <?php }?>
                    
                </select>
              </label>
            </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
              <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Setor Respons&aacute;vel:</span></td>
              <td>
                <label>
                  <select name="SETid_dest" id="SETid_dest" DISABLED>
                  	<?php
					$result_setor_dest = $GerenciaSetor->selectSetorTI($HELP_DEST_SETid);				
					foreach($result_setor_dest as $row_setor_dest){ 
						$SETid_dest 		= $row_setor_dest['SETid'];
						$SETsigla_dest 		= $row_setor_dest['SETsigla'];
					?>
						<option value="<?php echo "$SETid_dest"; ?>" <?php if($SETid_dest == $HELP_DEST_SETid){echo "SELECTED";} ?>><?php echo "$SETsigla_dest"; ?></option>
					<?php }?>                  
                  </select>
                </label>
              </td>
          </tr>
          <tr>
            <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Funcion&aacute;rio Respons&aacute;vel:</span></td>
            <td><label>
              <select name="FUNid_dest" id="FUNid_dest">
				<?php		
                $result_funcionario = $GerenciaFuncionario->selectFuncionario($ACE_FUNid, true);
                foreach($result_funcionario as $row_funcionario){
					$FUNid_dest      	= $row_funcionario["FUNid"];
					$FUNnome_dest   	= $row_funcionario["FUNnome"];
					$FUNsobrenome_dest	= $row_funcionario["FUNsobrenome"];				
					echo "<option value=\"$FUNid_dest\">$FUNnome_dest  $FUNsobrenome_dest</option>";
                }?>
              </select>
            </label></td>
          </tr>
          <tr>
            <td valign="top" bgcolor="#F1F4F8" class="texto style41">Resposta</td>
            <td>
                <label>
                    <span class="style50">
                        <textarea name="RESPconteudo" id="RESPconteudo" cols="35" rows="5"></textarea>
                    </span>
                </label>
            </td>
          </tr>
          <tr>
			<td valign="top" bgcolor="#F1F4F8" class="texto style41">Status Helpdesk:</td>
			<td>
               <label>
                    <select name="SITid" id="SITid">
                    <?php
                    $result_situacao = $GerenciaSituacao->selectSituacao();
                    foreach($result_situacao as $row_situacao){ 
						$SITid 		  = $row_situacao['SITid'];
						$SITdescricao = $row_situacao['SITdescricao'];
                    	echo "<option value=\"$SITid\">$SITdescricao</option>";
					}
					?>
                    
                    </select>
				</label>
			</td>
        </tr>
          <tr>
              <td class="texto">&nbsp;</td>
              <td>&nbsp; </td>
          </tr>
          <tr>
              <td class="texto style42">&nbsp;</td>
              <td>
              	 <input name="Cadastrar" type="submit" id="Cadastrar" value="Confirmar" class="caixa_texto">&nbsp;&nbsp;&nbsp;
                 <input name="voltar" type="button" id="voltar" value="Voltar" class="caixa_texto" OnClick="location.href='../cadastro/formulario_abertos.php'"/>              </td>
          </tr>
	  </table>
    </form>
</div>
</body>
</html>
<?php }?>